package gov.gfbzb.advice.controller;

import gov.gfbzb.advice.annotation.AuthCheck;
import gov.gfbzb.advice.controller.dto.AdviceDTO;
import gov.gfbzb.advice.controller.dto.AdviceUpdateDTO;
import gov.gfbzb.advice.exception.BusinessException;
import gov.gfbzb.advice.exception.ErrorCode;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import gov.gfbzb.advice.util.Result;
import gov.gfbzb.advice.service.IAdviceService;
import gov.gfbzb.advice.entity.Advice;

import org.springframework.web.bind.annotation.RestController;

/**
 * AdviceController 前端控制器
 * @author 小宇
 * @since 2025-07-05
 */
@Log4j2
@RestController
@Tag(name = "咨询问答控制器")
@RequestMapping("/advice")
public class AdviceController {

    @Autowired
    private IAdviceService adviceService;

    /**
    *  用户咨询
    */
    @PostMapping("/consult")
    @Operation(summary = "用户咨询")
    public Result consult(@RequestBody AdviceDTO adviceDTO) {
        // 非空判断
        if (StringUtils.isAnyBlank(adviceDTO.getTitle(),adviceDTO.getGender(), adviceDTO.getUserName(), adviceDTO.getIdCard(), adviceDTO.getPhone(), adviceDTO.getCity(), adviceDTO.getCounty())){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        adviceService.saveAndGetAdvice(adviceDTO);
        return Result.success("请求成功，等待回复");
    }

    /**
     *  查看——增加浏览次数
     * @param id id
     * @return Result
     */
    @GetMapping("/view")
    @Operation(summary = "查看——增加浏览次数")
    public Result view(@RequestParam(name = "id") Integer id){
        Advice advice = adviceService.getById(id);
        if (advice == null){
            return Result.error("未找到该数据");
        }
        // 增加浏览次数
        advice.setViewCount(advice.getViewCount() + 1);
        if (adviceService.updateById(advice)){
            return Result.success("成功");
        }
        return Result.error("失败");
    }

    /**
     * 编辑答复内容保存
     * @param adviceUpdateDTO adviceUpdateDTO
     * @return Result
     */
    @AuthCheck(mustAdmin = "admin")
    @PostMapping("/updateAnswer")
    @Operation(summary = "编辑答复内容保存")
    public Result updateAnswer(@RequestBody AdviceUpdateDTO adviceUpdateDTO){
        // 通过id 获取
        Advice advice = adviceService.getById(adviceUpdateDTO.getId());
        // 更新属性
        advice.setAnswer(adviceUpdateDTO.getAnswer());
        boolean bool = adviceService.updateById(advice);
        log.info("咨询序号为{}的答复内容被保存", adviceUpdateDTO.getId());
        if (bool){
            return Result.success("保存成功");
        }
        return Result.error("保存失败");
    }

    /**
     * 通过审核
     * @param id 咨询id
     * @return Result
     */
    @AuthCheck(mustAdmin = "admin")
    @GetMapping("/passAdvice")
    @Operation(summary = "通过审核")
    public Result passAdvice(@RequestParam(name = "id") Integer id,@RequestParam(name = "verifyName") String verifyName){
        // 通过id 获取
        Advice advice = adviceService.getById(id);
        // 更新属性
        advice.setQuestionStatus(1);
        advice.setVerifyName(verifyName);
        advice.setVerifyTime(LocalDateTime.now());
        boolean bool = adviceService.updateById(advice);
        log.info("咨询序号为{}的答复内容被审核通过", id);
        if (bool){
            return Result.success("审核通过成功");
        }
        return Result.error("失败");
    }

    @AuthCheck(mustAdmin = "admin")
    @GetMapping("/rejected")
    @Operation(summary = "审核驳回")
    public Result rejected(@RequestParam(name = "id") Integer id,@RequestParam(name = "rejectName") String rejectName){
        Advice advice = adviceService.getById(id);
        advice.setQuestionStatus(2);
        advice.setRejectName(rejectName);
        advice.setRejectTime(LocalDateTime.now());
        boolean bool = adviceService.updateById(advice);
        log.info("咨询序号为{}的答复内容被{}审核驳回", id,rejectName);
        if (bool){
            return Result.success("通过");
        }
        return Result.error("失败");
    }
    /**
     * 通过id获取
     * @param id id
     * @return advice
     */
    @GetMapping("/{id}")
    @Operation(summary = "通过id获取")
    public Result getById(@PathVariable(name = "id") Integer id) {
        QueryWrapper<Advice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        queryWrapper.eq("is_delete", 0);
        Advice advice =adviceService.getOne(queryWrapper);
        if (advice == null){
            return Result.error("未找到该数据");
        }
        return Result.success(advice);
    }

    /**
    *  删除
    */
    @GetMapping("/delete")
    @AuthCheck(mustAdmin = "admin")
    @Operation(summary = "通过id删除")
    public Result delete(@RequestParam(name = "id") Integer id) {
        Advice advice =adviceService.getById(id);
        advice.setIsDelete(1);
        return Result.success(adviceService.saveOrUpdate(advice));
    }

    /**
     * 分页查询所有已审核通过的咨询
     * @param pageNum 页数
     * @param pageSize 每页大小
     * @param viewCount 是否按浏览次数排序
     * @param county 区县模糊
     * @param keyWord 关键字
     * @param cardOrPhone 身份证或手机号
     * @return Page<Advice>
     */
    @GetMapping("/pageAllPass")
    @Operation(summary = "分页查询所有已审核通过的咨询")
    public Result pageAllPass(@RequestParam(name = "pageNum") Integer pageNum,
                              @RequestParam(name = "pageSize") Integer pageSize,
                              @RequestParam(name = "viewCount", required = false) Boolean viewCount,
                              @RequestParam(name = "county", required = false) String county,
                              @RequestParam(name = "keyWord", required = false) String keyWord,
                              @RequestParam(name = "cardOrPhone", required = false) String cardOrPhone) {

        Map<String, Object> stringObjectMap = adviceService.pageCondition(pageNum, pageSize, viewCount, county, keyWord ,cardOrPhone, true);
        return Result.success(stringObjectMap);
    }

    /**
     * 分页查询所有待审核的咨询
     * @param pageNum 页数
     * @param pageSize 每页大小
     * @param viewCount 是否按浏览次数排序
     * @param county 区县模糊
     * @param keyWord 关键字
     * @return Page<Advice>
     */
    @GetMapping("/pending")
    @AuthCheck(mustAdmin = "admin")
    @Operation(summary = "分页查询所有待审核的咨询")
    public Result pending(@RequestParam(name = "pageNum") Integer pageNum,
                          @RequestParam(name = "pageSize") Integer pageSize,
                          @RequestParam(name = "viewCount", required = false) Boolean viewCount,
                          @RequestParam(name = "county", required = false) String county,
                          @RequestParam(name = "keyWord", required = false) String keyWord,
                          @RequestParam(name = "cardOrPhone", required = false) String cardOrPhone) {

        Map<String, Object> stringObjectMap = adviceService.pageCondition(pageNum, pageSize, viewCount, county ,keyWord ,cardOrPhone, false);
        return Result.success(stringObjectMap);
    }
}

